<template>
  <div class="container">
    <Breadcrumb :items="['menu.survey', 'menu.survey.searchTable']" />
    <a-card class="general-card" :title="$t('menu.survey.searchTable')">
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="name" label="问卷名称">
                  <a-input
                    v-model="formModel.name"
                    placeholder="请输入问卷名称……"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="status" label="问卷状态">
                  <a-select
                    v-model="formModel.status"
                    :options="statusOptions"
                    placeholder="全部"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>

        <a-divider style="height: 42px" direction="vertical" />

        <a-col :flex="'192px'" style="text-align: right">
          <a-space direction="horizontal" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              {{ $t('searchTable.form.search') }}
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              {{ $t('searchTable.form.reset') }}
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <!--新增和导入-->
      <a-row style="margin-bottom: 16px">
        <a-col :span="16">
          <a-space>
            <a-button type="primary">
              <template #icon>
                <icon-plus />
              </template>
              {{ $t('searchTable.operation.create') }}
            </a-button>
            <a-upload action="/">
              <template #upload-button>
                <a-button>
                  {{ $t('searchTable.operation.import') }}
                </a-button>
              </template>
            </a-upload>
          </a-space>
        </a-col>
        <!--下载-->
        <!-- <a-col :span="8" style="text-align: right">
          <a-button>
            <template #icon>
              <icon-download />
            </template>
            {{ $t('searchTable.operation.download') }}
          </a-button>
        </a-col> -->
      </a-row>
      <!--这是展示的地方-->
      <a-table
        row-key="id"
        :loading="loading"
        :pagination="pagination"
        :data="renderData"
        :bordered="false"
        @page-change="onPageChange"
      >
        <template #columns>
          <a-table-column title="问卷编号" data-index="id" />
          <a-table-column title="问卷名" data-index="name" />
          <!-- <a-table-column title="问卷描述" data-index="description">
          </a-table-column> -->
          <a-table-column title="问卷拥有者" data-index="managedBy">
            <!--<template #cell="{ record }">-->
            <!--  {{ $t(`searchTable.form.filterType.${record.filterType}`) }}-->
            <!--</template>-->
          </a-table-column>
          <!-- <a-table-column title="开始时间" data-index="startTime" />
          <a-table-column title="结束时间" data-index="endTime" /> -->
          <a-table-column title="答卷上限" data-index="answerLimit">
            <!--<template #cell="{ record }">-->
            <!--  <span v-if="record.status === 'offline'" class="circle"></span>-->
            <!--  <span v-else class="circle pass"></span>-->
            <!--  {{ $t(`searchTable.form.status.${record.status}`) }}-->
            <!--</template>-->
          </a-table-column>
          <a-table-column title="操作" data-index="operations">
            <template #cell="record">
              <a-button
                type="text"
                status="success"
                size="small"
                @click="preview(record)"
              >
                回答
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { useSurveyStore } from '@/store';
  import { computed, ref, reactive } from 'vue';
  import { useI18n } from 'vue-i18n';
  import {
    deleteQuestion,
    previewsurvey,
    surveyList,
    PolicyRecord,
    PolicyParams,
    deleteSurvey,
  } from '@/api/list';

  import useLoading from '@/hooks/loading';

  import { Pagination } from '@/types/global';
  import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
  // import {previewSurvey} from '@/api/list'
  // eslint-disable-next-line no-redeclare
  import router from '@/router';
  // eslint-disable-next-line no-redeclare

  // 解析不了

  // 查询往后传的东西
  const toBack = () => {
    return {
      survey_title: '',
      survey_state: '',
    };
  };

  //
  const previewVisible = ref(false);
  const dialogTableValue = ref();
  // 展示的东西
  const generateFormModel = () => {
    return {
      number: '',
      name: '',
      description: '',
      startTime: '',
      endTime: '',
      status: '',
    };
  };
  const { loading, setLoading } = useLoading(true);
  const { t } = useI18n();
  const renderData = ref<PolicyRecord[]>([]);
  const formModel = ref(generateFormModel());
  const basePagination: Pagination = {
    page: 1,
    limit: 20,
  };
  const pagination = reactive({
    ...basePagination,
  });
  const contentTypeOptions = computed<SelectOptionData[]>(() => [
    {
      label: t('searchTable.form.contentType.img'),
      value: 'img',
    },
    {
      label: t('searchTable.form.contentType.horizontalVideo'),
      value: 'horizontalVideo',
    },
    {
      label: t('searchTable.form.contentType.verticalVideo'),
      value: 'verticalVideo',
    },
  ]);
  const filterTypeOptions = computed<SelectOptionData[]>(() => [
    {
      label: t('searchTable.form.filterType.artificial'),
      value: 'artificial',
    },
    {
      label: t('searchTable.form.filterType.rules'),
      value: 'rules',
    },
  ]);
  const statusOptions = computed<SelectOptionData[]>(() => [
    {
      label: '全部',
      value: 'all',
    },
    {
      label: '已发布',
      value: 'online',
    },
    {
      label: '未发布',
      value: 'offline',
    },
  ]);

  const fetchData = async (
    params: PolicyParams = { page: 1, limit: 20, name: formModel.value.name }
  ) => {
    setLoading(true);
    try {
      const { data } = await surveyList(params);
      renderData.value = data.list;
      pagination.page = params.page;
      pagination.total = data.total;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const search = () => {
    fetchData({
      ...basePagination,
      ...formModel.value,
    } as unknown as PolicyParams);
  };
  const onPageChange = (page: number) => {
    fetchData({ ...basePagination, page });
  };

  fetchData();
  const reset = () => {
    formModel.value = generateFormModel();
  };
  // const modifyQ = () => {

  // }

  const deleteS = async (record) => {
    // console.log(record.id);
    await deleteSurvey([record.record.id]);
  };
  const analys = (record) => {
    router.push({
      name: 'ScreenMultiDimensionDataAnalysis',
    });
  };
  // 预览那个按钮
  // 先写发请求吧
  const preview = (record) => {
    useSurveyStore.$id = record.record.id;
    useSurveyStore().from = 0;
    router.push({ name: 'AnserQuestion' });
    // previewVisible.value = true;
    // console.log('赋值')
    // console.log(dialogTableValue.value)
    // console.log(record.record.id)
    // 怎么发请求啊无语死了
    // await previewsurvey([record.record.id]);
  };
</script>

<script lang="ts">
  export default {
    name: 'SearchTable',
  };
</script>

<style scoped lang="less">
  .container {
    padding: 0 20px 20px 20px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
